﻿#include "UiPluginWidget.h"
#include "ui_UiPluginWidget.h"

#include "WorkThread.h"
#include "CustomListModel.h"
#include "CustomListItemDelegate.h"
#include "CustomDelegate.h"

#include <QDebug>
#include <QListView>
#include <QTreeView>
#include <QTableView>
#include <QHeaderView>
#include <QStandardItemModel>

UiPluginWidget::UiPluginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UiPluginWidget)
{
    ui->setupUi(this);

    setupUi();

    setupConnect();
}

UiPluginWidget::~UiPluginWidget()
{
    delete ui;
}


void UiPluginWidget::setupUi()
{
    /*-------------------------状态灯（自定义）-------------------------------*/
    initStatusLight();

    /*-------------------------沙漏（自定义）-------------------------------*/
    initHourGlass();

    /*-------------------------List-------------------------------*/
    initListView();

    /*-------------------------Tree-------------------------------*/
    initTreeView();

    /*-------------------------Table-------------------------------*/
    initTableView();

    /*-------------------------NetworkHostInfo-------------------------------*/
    networkHostInfo_ = new NetworkHostInfo();
    ui->stackedWidget->addWidget(networkHostInfo_);
    ui->listWidget->addItem(QStringLiteral("网络主机信息"));
}

void UiPluginWidget::setupConnect()
{
    // 连接listWidget的项目切换显示对应的页面
    connect(ui->listWidget, &QListWidget::currentRowChanged, this, &UiPluginWidget::slotWidgetSwitch);
}

void UiPluginWidget::slotWidgetSwitch(int index)
{
    //    qDebug() << "current index changed: " << index;
    ui->stackedWidget->setCurrentIndex(index);
}

void UiPluginWidget::initStatusLight()
{
    statusLightWidget = new StatusLightWidget(this);
    ui->stackedWidget->addWidget(statusLightWidget);
    ui->listWidget->addItem(QStringLiteral("状态灯"));
}

void UiPluginWidget::initHourGlass()
{
    hourGlassWidget_ = new HourGlassWidget(this);
    ui->stackedWidget->addWidget(hourGlassWidget_);
    ui->listWidget->addItem(QStringLiteral("沙漏"));
}

void UiPluginWidget::initListView()
{
    listView_ = new QListView(this);
    ui->stackedWidget->addWidget(listView_);
    ui->listWidget->addItem(QStringLiteral("列表-ListView"));
    ui->listWidget->setAlternatingRowColors(true);
    listModel_ = new CustomListModel();
    listView_->setModel(listModel_);
    listView_->setItemDelegate(new CustomListItemDelegate());
    listView_->setSelectionMode(QAbstractItemView::SingleSelection);
    listView_->setAlternatingRowColors(true);

    DataItem item;
    item.type = DataType::StringType;
    item.value = QStringLiteral("123");
    listModel_->addItem(item);
    listModel_->addItem(item);
    listModel_->addItem(item);
}

void UiPluginWidget::initTreeView()
{
    QTreeView *treeView = new QTreeView(this);
    treeView->setRootIsDecorated(false);
    // 创建模型
    // 需要初始化时为模型设置好数据
    // 4行4列
    QStandardItemModel *model = new QStandardItemModel(this);
    model->setColumnCount(4);
    // 设置表头
    model->setHeaderData(0, Qt::Horizontal, "Name");
    model->setHeaderData(1, Qt::Horizontal, "Type");
    model->setHeaderData(2, Qt::Horizontal, "Count");
    model->setHeaderData(3, Qt::Horizontal, "Price");

    QJsonObject jsonData;
    jsonData["Name"] = "Apple";
    QJsonArray typeList;
    typeList.append("Fruit");
    typeList.append("Carrot");
    typeList.append("Meat");
    typeList.append("Drink");

    jsonData["Type"] = typeList;
    jsonData["Count"] = 10;
    jsonData["Price"] =  5.2;

    addTreeRow(model, nullptr, jsonData);
    addTreeRow(model, nullptr, jsonData);

    CustomDelegate *delegate = new CustomDelegate(this);

    // 注册工厂
    delegate->registerFactory(0, new LineEditFactory());
    delegate->registerFactory(1, new ComboBoxFactory());
    delegate->registerFactory(2, new SpinBoxFactory());
    delegate->registerFactory(3, new DoubleSpinBoxFactory());

    treeView->setModel(model);
    treeView->setItemDelegate(delegate);
    // 启用编辑
    treeView->setEditTriggers(QAbstractItemView::DoubleClicked |
                              QAbstractItemView::EditKeyPressed);
    treeView->header()->setSectionResizeMode(QHeaderView::Stretch);

    ui->stackedWidget->addWidget(treeView);
    ui->listWidget->addItem(QStringLiteral("Tree"));
}

void UiPluginWidget::initTableView()
{
    QTableView *tableView = new QTableView(this);
    // 创建模型
    // 需要初始化时为模型设置好数据
    QStandardItemModel *tableModel = new QStandardItemModel(this);
    tableModel->setColumnCount(4);
    // 设置表头
    tableModel->setHeaderData(0, Qt::Horizontal, "Name");
    tableModel->setHeaderData(1, Qt::Horizontal, "Type");
    tableModel->setHeaderData(2, Qt::Horizontal, "Count");
    tableModel->setHeaderData(3, Qt::Horizontal, "Price");

    QJsonObject jsonTableData;
    jsonTableData["Name"] = "Apple";

    QJsonArray tableTypeList;
    tableTypeList.append("Fruit");
    tableTypeList.append("Carrot");
    tableTypeList.append("Meat");
    tableTypeList.append("Drink");
    jsonTableData["Type"] = tableTypeList;

    jsonTableData["Count"] = 10;
    jsonTableData["Price"] =  5.2;

    addTableRow(tableModel, jsonTableData);

    CustomDelegate *tableDelegate = new CustomDelegate(this);
    tableDelegate->registerFactory(0, new LineEditFactory());
    tableDelegate->registerFactory(1, new ComboBoxFactory());
    tableDelegate->registerFactory(2, new SpinBoxFactory());
    tableDelegate->registerFactory(3, new DoubleSpinBoxFactory());

    tableView->setModel(tableModel);
    tableView->setItemDelegate(tableDelegate);
    tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    // 隐藏列标题
    tableView->verticalHeader()->hide();

    ui->stackedWidget->addWidget(tableView);
    ui->listWidget->addItem(QStringLiteral("Table"));
}

void UiPluginWidget::addTreeRow(QStandardItemModel *model, QStandardItem *parentItem, const QJsonObject &json)
{
    QList<QStandardItem*> cols;
    cols << new QStandardItem(json["Name"].toString());
    QStringList typeList;
    for(const auto json : json["Type"].toArray())
    {
        typeList.append(json.toString());
    }
    // 默认选择第0个
    auto typeItem = new QStandardItem(typeList.size() > 0 ? typeList[0] : "new item");
    typeItem->setData(typeList, Qt::UserRole + 1);
    cols << typeItem;

    cols << new QStandardItem(QString::number(json["Count"].toInt()));
    cols << new QStandardItem(QString::number(json["Price"].toDouble()));

    if (parentItem) {
        // 挂到某个父节点下
        parentItem->appendRow(cols);
    } else {
        // 顶层节点
        model->appendRow(cols);

    }
}

void UiPluginWidget::addTableRow(QStandardItemModel *model, const QJsonObject &json)
{
    QList<QStandardItem*> colDatas;
    colDatas << new QStandardItem(json["Name"].toString());

    QStringList typeList;
    for(const auto json : json["Type"].toArray())
    {
        typeList.append(json.toString());
    }
    // 默认选择第0个
    auto typeItem = new QStandardItem(typeList.size() > 0 ? typeList[0] : "new item");
    typeItem->setData(typeList, Qt::UserRole + 1);
    colDatas << typeItem;

    colDatas<< new QStandardItem(QString::number(json["Count"].toInt()));
    colDatas<< new QStandardItem(QString::number(json["Price"].toDouble()));

    model->appendRow(colDatas);
}
